<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_documents_RootNode extends m_backoffice_documents_RootNodeBase
{
	/**
	 * @return m_backoffice_documents_RootNode
	 */
	public static function createNew()
	{
		return new self();
	}
	
	/**
	 * @return m_backoffice_documents_RootNode
	 */
	public static function getInstanceByUniqueId($id, $lang = null)
	{
		if(is_null($lang)) $lang = f_core_Context::getInstance()->getLang();
		$cache = f_core_Cache::getInstance();
		
		$key = $cache->getDocumentCacheKey($id, $lang);
		
		if($cache->hasCache($key))
		{
			return $cache->getCacheContent($key);
		}
		
		$object = new self($id, $lang);
		
		$cache->putInCache($key, $object);
		
		return $object;
	}
	
	public function __call($method, $attr)
	{
		$prefix = substr($method, 0, 3);
		$suffix = strtolower(substr($method, 3));

        $relationManager = f_relation_ManagerNew::getInstance($this);
				
		if($prefix == "add")
		{
			$relationManager->addRelation($attr[0], $suffix);
			return $this;
		}
		
		if($prefix == "get")
		{
			if(substr($suffix, -2, 2) == "nb")
			{
				return $relationManager->getChilds()->withRelationType(substr($suffix, 0, -2))->withLang($this->getLang())->count();
			}
			
			return $relationManager->getChilds()->withRelationType($suffix)->withLang($this->getLang())->orderBy('ASC')->retrieveDocuments();
		}
	}
	
	public function canSlide()
	{
		return true;
	}

    private function getTreeParserFilePath()
    {
        return MODULES_DIR.DIRECTORY_SEPARATOR.$this->getModule().DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR."treeParser.xml";
    }

	public function getLabel()
	{		
		return f_xml_Xpath::getInstance($this->getTreeParserFilePath())->fromXpath("rootNode")->getAttributeValue('label');
	}	
	
	public function getIcon()
	{
		return f_xml_Xpath::getInstance($this->getTreeParserFilePath())->fromXpath("rootNode")->getAttributeValue('icon');
	}
	
	public function getEditUrl()
	{
        $xml = f_xml_Xpath::getInstance($this->getTreeParserFilePath())->fromXpath("rootNode");

        $route = $xml->getAttributeValue('route');
        
        $docId = $this->getUniqueId();

        $rootNode = $xml->getAttributeValue('rootnode');

        if($rootNode == 'website')
        {
            $websites = $this->getAllWebsites();

            if(count($websites) == 1)
            {
                $docId = $websites[0]->getUniqueId();
            }
        }

		return f_core_Routing::getUrl($route, array("configModule" => $this->getModule(), "docLang" => strtolower($this->getLang()), "docId" => $docId));
	}

	/**
	 * Retrieve all websites for all langs for current rootNode
	 * @return array
	 */
	public function getAllWebsites()
	{
		$dbProvider = f_database_Provider::getInstance();
		
		$sql = "SELECT `document_uid`, `document_lang` FROM `m_website__website`";
		
		$results = $dbProvider->setQuery($sql)->execute()->getQueryResults(PDO::FETCH_ASSOC);
		
		$websites = array();
		
		if(count($results) > 0)
		{
			$documentProvider = f_document_Provider::getInstance();
			
			foreach ($results as $result)
			{
				$websites[] = $documentProvider->getByUniqueId($result["document_uid"], $result["document_lang"]);
			}
		}
		
		return $websites;
	}
	
	/**
	 * Return an array with all websites langs
	 * @return array
	 */
	public function getAllWebsitesLangs()
	{
		$dbProvider = f_database_Provider::getInstance();
		
		$sql = "SELECT `document_lang` FROM `m_website__website`";
		
		return array_unique($dbProvider->setQuery($sql)->execute()->getQueryResults(PDO::FETCH_COLUMN));
	}
}